import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import ELEMENT from 'element-ui'
import store from './store/index'
import base from './config'
import echarts from 'echarts';
import  VueQuillEditor from 'vue-quill-editor'
import {format} from '@/public/js/util'
import {
  has
} from './public/js/util'
import directives from './public/directive/directive'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.config.productionTip = false

Vue.use(VueQuillEditor)
window.bus = new Vue({})

window.commo = {
  accountInformation: {},
  hotelInformation: {},
  token: sessionStorage.token,
  companyId: sessionStorage.companyId,
  defaultParameter: {
    timestamp: new Date().getTime(),
    version: 'string',
    operation: 'string'
  },
  uuid: sessionStorage.getItem('uuid'),
  qiniuUrl: 'http://upload-z2.qiniu.com',
  qiniuImageUrl: 'http://ozjs8a6dq.bkt.clouddn.com/',
  url: 'http://192.168.10.253:8088/',
  time: () => {
    const date = new Date()
    let year = date.getFullYear()
    let mouth = date.getMonth() + 1
    let day = date.getDate()
    if (mouth > 12) mouth = 1
    let time = year + '-' + mouth + '-' + day
    return time
  },
  showMenu: (obj) => {
    const Menu = JSON.parse(sessionStorage.Menu)
    Menu.forEach(item => {
      obj[item.functionId] = false
      if (item.functionId !== '') obj[item.functionId] = true
    })
  }
}

const messageInformation = ELEMENT.Message

axios.defaults.timeout = 100000
let {
  baseApi
} = base;
//axios.defaults.baseURL = 'http://118.190.205.4:8088/'
axios.defaults.baseURL = baseApi

axios.interceptors.request.use(config => {
  return config
}, function (error) {
  // loading.close()
  return Promise.reject(error)
})
axios.interceptors.response.use(res => {
  if (res.data.errCode) {
    if (res.data.errCode !== 0) {
      console.log(res.data.errCode)
      messageInformation.error({
        message: '出错了 ' + res.data.errMsg
      })
      if (res.data.errCode === 10001) {
        router.push({
          path: '/'
        })
      }
    }
  } else {
    if (res.status) {
      if (res.status !== 200) {
        messageInformation.error({
          message: '出错了 '
        })
      }
    }
  }
  return res
}, function (error) {
  return Promise.reject(error)
})

Vue.prototype.$http = axios;

Vue.prototype.$echarts = echarts
window.$http = axios

Vue.config.productionTip = false

Vue.use(ELEMENT)
Vue.use(directives)

window.fetch = function (options) {
  return window.$http({
    method: options.method || 'GET',
    url: options.url,
    params: {
      token: window.commo.token
    },
    data: Object.assign({}, {
      timestamp: Date.parse(new Date()),
      version: 'string',
      operation: 'string'
    }, options.data)
  })
}
//定义指令 控制权限

//console.log(sessionStorage.getItem('Menu'))

/* eslint-disable no-new */
const vue = new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: {
    App
  },
  data: {
    str: '111'
  }
})
Vue.directive('has', {
  bind: function (el, binding) {
    if (!has(binding.value, vue)) {
      el.style.display = "none"
    }
  }
});

Vue.directive('numberOnly', {
  inserted: function (el) {
    el.addEventListener("keypress", function (e) {
      console.log(e)
      e = e || window.event;
      let charcode = typeof e.charCode == 'number' ? e.charCode : e.keyCode;
      let re = /\d/;
      if (charcode == 46) {
        if (el.value.includes('.')) {
          e.preventDefault();
        }
        return;
      } else if (!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey) {
        if (e.preventDefault) {
          e.preventDefault();
        } else {
          e.returnValue = false;
        }
      }
    });
  }

})
